#!/usr/bin/env python3
# 直接调试QuestionService，避免API层干扰

from app.core.database import SessionLocal
from app.services.question_service import QuestionService
from app import schemas

def debug_service():
    print("=== 直接调试QuestionService ===")
    
    db = SessionLocal()
    try:
        service = QuestionService(db)
        
        # 测试配置
        config = schemas.TestConfig(
            grade_id=1,
            question_count=5,
            time_limit=30
        )
        
        print("1. 调用generate_test_paper...")
        result = service.generate_test_paper(config)
        print("   成功! 试卷ID:", result.id)
        
        print("2. 调用submit_test...")
        # 创建一个简单的测试答案（字典格式），包含用户ID
        answers = schemas.TestAnswers(
            answers={1: "A", 2: "B", 3: "C", 4: "A", 5: "B"},
            user_id=1  # 提供一个有效的用户ID
        )
        test_result = service.submit_test(result.id, answers)
        print("   成功! 测试结果分数:", test_result.score)
        
        return True
        
    except Exception as e:
        print("错误:", str(e))
        import traceback
        traceback.print_exc()
        return False
        
    finally:
        db.close()

if __name__ == "__main__":
    debug_service()